// pages/login/login.js
const app =getApp({})
Page({

    /**
     * 页面的初始数据
     */
    data: {
        username:"",
        userpassword:"",
        userInputPla:"请输入手机号",
    },
    /* 跳转到注册页面 */
    NavToRegister(){
        wx.navigateTo({
        url: '/pages/register/register',
        })
    },
    /* 获取账号 */
    userInput(e){
        console.log(e.detail.value);
        this.setData({
            username:e.detail.value
        })
    },
    /* 获取密码 */
    pwdInput(e){
        console.log(e.detail.value);
        this.setData({
            userpassword:e.detail.value
        })
    },
    /* login */
    login(){
        if(this.data.username.length==0 || this.data.userpassword.length==0){
            wx.showToast({
              title: '信息填写完整',
              icon:"error"
            })
            return
        }
        wx.request({
          url: app.globalData.baseUrl+'/mini/login',
          method:"POST",
          data:{
            user:this.data.username,
            password:this.data.userpassword
          },success:(res)=>{
              console.log(res);
              if(res.data.status=="ok"){
                  wx.setStorageSync('userinfo', res.data.data)
                  wx.switchTab({
                    url: '/pages/index/index',
                  })
              }else{
                wx.showToast({
                    title: res.data.mgs,
                    icon:"none"
                  })
              }
          },fail:(res)=>{
              console.log(res);
          }
        })
    },
    /**
     * 生命周期函数--监听页面显
     */
    onShow() {
        if(wx.getStorageSync('userinfo')){
            wx.showModal({
              title: '您已登录',
              content: '无需重复登录',
              complete: (res) => {
                if (res.confirm) {
                    wx.switchTab({
                      url: '/pages/index/index',
                    })
                }if(res.cancel){
                    wx.switchTab({
                        url: '/pages/index/index',
                      })
                }
              }
            })
        }
    },
    navToMwLogin(){
        wx.navigateTo({
          url: '/pages/scanLogin/scanLogin',
        })
    }
})